const app = new Vue({
    el: '#selBox',
    data: function () {
        return {
            isShow: 1,
            form1: {
                drugName: [],
                manName: [],
                batchNum: [],
                traceableCode: '',
                ard: '', //到达日期
                other: ''
            },
            selectForm: {
                drugName: '',
                manName: '',
                batchNum: ''
            },
            sub1: {
                drugName: '',
                manName: '',
                batchNum: '',
                traceableCode: '',
                ard: '',
                other: ''
            },
            form2: {
                consumer: '',
                sex: '0',
                idNumber: '',
                contact: '',
                drugName: [],
                cond: '', //购买日期
                batchNum: [],
                traceableCode: '',
                other: ''
            },
            selectCon: {
                drugName: '',
                batchNum: ''
            },
            sub2: {
                consumer: '',
                sex: '',
                idNumber: '',
                contact: '',
                drugName: '',
                cond: '',
                batchNum: '',
                traceableCode: '',
                other: ''
            },
            sch1: {
                traceableCode: '',
                drugName: '',
                batchNum: '',
                beginDate: '',
                endDate: ''
            },
            sch2: {
                traceableCode: '',
                drugName: '',
                batchNum: '',
                consumer: '',
                beginDate: '',
                endDate: ''
            },
            selTable: [],
            conTable: [],
            form4: {
                userId: '',
                username: '',
                phoneNumber: '',
                selName: '',
                address: '',
                legal: '',
                idNumber: ''
            },
            pwdForm: {
                oldPwd: '',
                newPwd: ''
            },
            phoForm: {
                param: ''
            },
            pwdOpen: false,
            phoOpen: false,
            formLabelWidth: '120px'
        }
    },
    methods: {
        change1: function () {
            this.isShow = 1;
            // 获取所有药品名称
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.get('/info/drugTra', {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.form1.drugName = res.data.data;
                    } else if (code === 1004) {
                        console.log(token);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        change2: function () {
            this.isShow = 2;
            // 获取所有药品名称
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.get('/info/drugCon', {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.form2.drugName = res.data.data;
                    } else if (code === 1004) {
                        console.log(token);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        change3: function () {
            this.isShow = 3;
        },
        change4: function () {
            this.isShow = 4;
        },
        change5: function () {
            let that = this;
            that.isShow = 5;
            let token = window.localStorage.getItem('userToken');
            axios.get('/info/getSel', {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    console.log(res);
                    if (code === 200) {
                        that.form4.username = res.data.data.username;
                        that.form4.phoneNumber = res.data.data.phoneNumber;
                        that.form4.selName = res.data.data.selName;
                        that.form4.address = res.data.data.address;
                        that.form4.legal = res.data.data.legal;
                        that.form4.idNumber = res.data.data.idNumber;
                    } else if (code === 1004) {
                        console.log(token);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        logout: function () {
            let token = window.localStorage.getItem('userToken');
            axios.get('/logout', {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        location.href = '../../index.html';
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        cleanForm: function () {
            if (this.isShow === 3) {
                this.sch1 = {
                    traceableCode: '',
                    drugName: '',
                    batchNum: '',
                    beginDate: '',
                    endDate: ''
                }
            }
            if (this.isShow === 4) {
                this.sch2 = {
                    traceableCode: '',
                    drugName: '',
                    batchNum: '',
                    consumer: '',
                    beginDate: '',
                    endDate: ''
                }
            }
        },
        selectDrug: function () {
            //根据选择的药品名称，获取该药品对应的所有药品生产商名称
            let that = this;
            axios.post('/info/manName', {param: that.selectForm.drugName}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.form1.manName = res.data.data;
                    } else if (code === 1001) {
                        console.log(that.selectForm.drugName);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        selectManName: function () {
            //根据选择的药品生产商名称，获取该药品的所有药品批号
            let that = this;
            axios.post('/info/batchTra', {
                first: that.selectForm.drugName,
                second: that.selectForm.manName
            }).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.form1.batchNum = res.data.data;
                    } else if (code === 1001) {
                        console.log(that.selectForm.drugName);
                        console.log(that.selectForm.manName);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        getCode: function () {
            //根据前边的选择获取药品溯源编码
            let that = this;
            axios.post('/info/codeTra', that.selectForm).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.form1.traceableCode = res.data.data;
                    } else if (code === 1001) {
                        console.log(that.selectForm);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        onSubmit1: function () {
            //提交药品购买数据

            if (this.selectForm.drugName === ''
                || this.selectForm.manName === ''
                || this.selectForm.batchNum === ''
                || this.form1.traceableCode === ''
                || this.form1.ard === '') {
                this.$message.warning('请填写完整数据！');
                return;
            }
            this.sub1.drugName = this.selectForm.drugName;
            this.sub1.manName = this.selectForm.manName;
            this.sub1.batchNum = this.selectForm.batchNum;
            this.sub1.traceableCode = this.form1.traceableCode;
            this.sub1.ard = this.form1.ard;
            this.sub1.other = this.form1.other;

            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.post('/submit/seld', that.sub1,
                {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.$message.success('保存成功！');
                        that.selectForm = {
                            drugName: '',
                            manName: '',
                            batchNum: ''
                        }
                        that.form1.traceableCode = '';
                        that.form1.ard = '';
                        that.form1.other = '';
                    } else if (code === 1001) {
                        that.$message.warning('数据出错，未保存成功！');
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        selectDn: function () {
            // 根据选择的药品名称，获取对应的药品批号
            let that = this;
            axios.post('/info/batchCon', {param: that.selectCon.drugName}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.form2.batchNum = res.data.data;
                    } else if (code === 1001) {
                        console.log(that.selectForm.drugName);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        selectBn: function () {
            // 根据选择的药品批号，获取药品溯源码
            let that = this;
            axios.post('/info/codeCon', {
                first: that.selectCon.drugName,
                second: that.selectCon.batchNum
            }).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.form2.traceableCode = res.data.data;
                    } else if (code === 1001) {
                        console.log(that.selectCon);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        onSubmit2: function () {
            //提交消费者数据
            if (this.form2.consumer === ''
                || this.form2.idNumber === ''
                || this.form2.contact === ''
                || this.form2.cond === ''
                || this.form2.traceableCode === ''
                || this.selectCon.drugName === ''
                || this.selectCon.batchNum === '') {
                this.$message.warning('请填写完整数据！');
                return;
            }
            this.sub2.consumer = this.form2.consumer;
            this.sub2.sex = this.form2.sex;
            this.sub2.idNumber = this.form2.idNumber;
            this.sub2.contact = this.form2.contact;
            this.sub2.cond = this.form2.cond;
            this.sub2.traceableCode = this.form2.traceableCode;
            this.sub2.other = this.form2.other;
            this.sub2.drugName = this.selectCon.drugName;
            this.sub2.batchNum = this.selectCon.batchNum;

            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.post('/submit/cond', that.sub2,
                {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.$message.success('保存成功！');
                        that.selectCon = {
                            drugName: '',
                            batchNum: ''
                        }
                        that.form2.consumer = '';
                        that.form2.sex = '0';
                        that.form2.idNumber = '';
                        that.form2.contact = '';
                        that.form2.cond = '';
                        that.form2.traceableCode = '';
                        that.form2.other = '';
                    } else if (code === 1001) {
                        that.$message.warning('数据出错，未保存成功！');
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        search1: function () {
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.post('/search/seld', that.sch1,
                {headers: {'Authorization': token}}).then(
                function (res) {
                    console.log(res);
                    let code = res.data.code;
                    if (code === 200) {
                        that.selTable = [];
                        that.selTable = res.data.data;
                    } else if (code === 1001) {
                        console.log(that.sch1);
                    } else if (code === 1004) {
                        console.log(token);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        search2: function () {
            //溯源消费者数据
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.post('/search/cond', that.sch2,
                {headers: {'Authorization': token}}).then(
                function (res) {
                    console.log(res);
                    let code = res.data.code;
                    if (code === 200) {
                        that.conTable = [];
                        that.conTable = res.data.data;
                    } else if (code === 1001) {
                        console.log('参数错误');
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        cancelChangePwd: function () {
            this.pwdForm = {
                oldPwd: '',
                newPwd: ''
            }
            this.pwdOpen = false;
        },
        changePwd: function () {
            //修改密码
            if (this.pwdForm.oldPwd === '' || this.pwdForm.newPwd === '') {
                this.$message.warning('密码不能为空！');
                return;
            }
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.post('/info/changePwd', that.pwdForm,
                {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        that.$message.success('修改成功，请重新登录！');
                        location.href = '../../index.html';
                    } else if (code === 1007) {
                        that.$message.error('原始密码输入错误，请重试！');
                    }
                }, function (err) {
                    console.log(err);
                }
            )
            this.pwdOpen = false;
        },
        cancelChangePho: function () {
            this.phoForm.newPhone = '';
            this.phoOpen = false;
        },
        changePho: function () {
            //修改手机号码
            if (this.phoForm.param === '') {
                this.$message.warning('内容不能为空！');
                return;
            }
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.post('/info/changePho', that.phoForm,
                {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        that.$message.success('修改成功！');
                    } else if (code === 1001) {
                        console.log('参数错误');
                    }
                }, function (err) {
                    console.log(err);
                }
            )
            this.phoOpen = false;
        }
    },
    mounted: function () {
        // 页面加载时自动触发函数
        this.change1();
    }
})